package io.gitee.cengxiaoxiao.converters;

public abstract class AbstractDictConverter {

    /**
     * 字典码转换为字典值
     *
     * @param code 字典码
     * @return 字典值
     */
    protected abstract Object codeToText(Object code);

    /**
     * 字典值转换为字典码
     *
     * @param text 字典值
     * @return 字典码
     */
    protected abstract Object textToCode(Object text);

    /**
     * 转换
     *
     * @param value        待转换的值
     * @param isTextToCode 是否为字典值转换为字典码
     * @return 转换后的值
     */
    public Object convert(Object value, boolean isTextToCode) {
        if (value == null) {
            return null;
        }
        return isTextToCode ? textToCode(value) : codeToText(value);
    }
}
